<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_pingdd_yafee/model')->方法名();
*/
namespace jy_pingdd_yafee;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	/*
		确认拼单
	*/
	public function checkteam($goodsid,$teamid=0){
		if (!$teamid) {
			//判断团长是否是首次开团
			$hasteam=pdo_get('jy_weishop_groups_team',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'goodsid'=>$goodsid,'mid'=>$_SESSION['uid']));
			if($hasteam['id']){
				exi('您有一个该商品的拼单还在进行中！','error','jy_pingdd_yafee/teamdetail//teamid='.$hasteam['id']);
			}
		}
		$order = pdo_get('jy_weishop_order',array('mid'=>$_SESSION['uid'],'teamid >'=>0,'status'=>0,'uniacid' => $_SESSION['uniacid']));
		if ($order['id']) {
			$checkid = pdo_getcolumn('jy_weishop_order_goods',array('orderid'=>$order['id'],'goodsid' => $goodsid,'uniacid' => $_SESSION['uniacid']),'id');
			if ($checkid) {
				exi('您的该商品拼单已存在，请尽快完成支付！','error','jy_weishop/order.list/');
			}
		}
		if ($teamid) {
			$goods = pdo_get('jy_weishop_goods',array('id' => $goodsid,'uniacid' => $_SESSION['uniacid']));
			$num = pdo_count('jy_weishop_order',array('mid'=>$_SESSION['uid'],'status'=>1,'teamid'=>$teamid,'uniacid' => $_SESSION['uniacid']));
			if ($num == $goods['groupnum']) {
				exi('该拼单已完成，请参与其他拼单！','error','jy_pingdd_yafee/index/');
			}
		}
		return true;
	}
	/*
		拼团失败
	*/
	public function order_cancel($teamid){
		$params=array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid,'status'=>1);
		$order=pdo_getall('jy_weishop_order_ding',$params,array('id','mid','teamid','price','paytype'));
		foreach($order as $item){
			$check=model('jy_weishop/order/refund')->check_order_rerefund_ding(intval($item['id']));
			if(!$check){
				return false;
			}
			$params=array(
				'orderid'=>intval($item['id']),
				'images'=>$item['images']?ijson_encode($item['images']):'',
				'price'=>$item['price'],
				'content'=>'拼单失败返保证金',
			);
			model('jy_weishop/order/refund')->refund_create_ding($params,true);
			pdo_update('jy_weishop_order_ding',array('status'=>5),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
		}
		pdo_update('jy_weishop_groups_team',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	}
	/*
		拼团成功
	*/
	public function order_success($teamid){
		pdo_update('jy_weishop_groups_team',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
		$data=array(
			'status'=>2,
			'finishtime'=>TIMESTAMP,
		);
		$orders=pdo_getall('jy_weishop_order_ding',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid,'status'=>1));
		foreach($orders as $item){
			pdo_update('jy_weishop_order_ding',$data,array('id'=>$item['id']));
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']));
			$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid']));
			$item['finishtime']=TIMESTAMP;
			$item['goodstitle']=$goods['title'];
			$item['orderprice']=$order['price'];
			model('jy_weishop/notice')->notice_groupfinish_ding($item,$url);
		}
		return true;
	}
	/*
		支付成功返现
	*/
	public function order_fanxian($order,$pluginset=array()){
		$pluginset=$pluginset?:$this->plugin_setting('basic');
		$teamgoods=pdo_get('jy_pingdd_yafee_goods',array('goodsid'=>$order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$fanfee=$pluginset['fan_rate']*$order['price']/100;
		if($teamgoods['fan_rate']){
			$fanfee=$teamgoods['fan_rate']*$order['price']/100;
		}
		model('core/member')->add_credit2($order['mid'],$fanfee,'拼单购物返现','jy_pingdd_yafee');
		pdo_update('jy_weishop_order_ding',array('status'=>7),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		model('jy_weishop/notice')->notice_groupfinish_fan($fanfee,$order['id']);
		return true;
	}
	public function up_price($goods,$pluginset=array(),$isprice=false){
		$teamgoods=pdo_get('jy_pingdd_yafee_goods',array('goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		if($teamgoods['id'] && !$teamgoods['status']){
			exi('选择的商品不存在或已下架！','error','/index/');
		}
		$pluginset=$pluginset?:$this->plugin_setting('basic');
		$goods['ding_price']=$teamgoods['ding_price'];
		if($goods['groupprice']==0){
				$pluginset['groupdiscount']=$pluginset['groupdiscount']?:98;
				$goods['groupprice']=price($goods['price']*$pluginset['groupdiscount']/100);
		}
		if($pluginset['bao_type']==1){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_fee'];
		}else{
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_rate']*$goods['price']/100;
		}
		if($pluginset['bao_type']==1){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_fee'];
		}elseif($pluginset['bao_rate']){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_rate']*$goods['price']/100;
		}
		if($pluginset['groupdiscount']){
				$goods['groupprice']=$pluginset['groupdiscount']*$goods['price']/100;
		}
		$goods['grouptime']=$teamgoods['timeout']?:$pluginset['timeout'];
		$goods['grouptime']=$goods['grouptime']?:48;
		$goods['teamnum']=$teamgoods['teamnum']?:$pluginset['teamnum'];
		$goods['teamnum']=$goods['teamnum']?:2;
		$goods['groupprice']=$teamgoods['price']?:$goods['groupprice'];
		$goods['fan_price']=$pluginset['fan_rate']?$pluginset['fan_rate']*$goods['price']/100:0;
		$goods['fan_price']=$teamgoods['fan_rate']?$teamgoods['fan_rate']*$goods['price']/100:$goods['fan_price'];
		if($isprice){
			$goods['groupprice']=price($goods['groupprice']);
			$goods['fan_price']=price($goods['fan_price']);
			$goods['ding_price']=price($goods['ding_price']);
		}
		return $goods;
	}
}
?>